// subpkg_worker/breakfast/breakfast.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    elderId:'',
    stapleFood:
    [
       {
         id:1,
         name:'米饭',
         count:0
       },
      {
        id:2,
        name:'面条',
        count:0
      },
      {
        id:3,
        name:'饺子',
        count:0
      },

    ],
    assistedFood:
    [
      {
        id:1,
        name:'香蕉',
        count:0
      },
      {
        id:2,
        name:'饼干',
        count:0
      },
      {
        id:3,
        name:'面包',
        count:0
      },

    ],
    meatDishs:
    [
      {
        id:1,
        name:'鱼肉',
        count:0
      },
     {
       id:2,
       name:'鸡肉',
       count:0
     },
     {
       id:3,
       name:'鸭肉',
       count:0
     },
     {
       id:4,
       name:'猪肉',
       count:0
     }
    ],
    vegetableDishs:
    [
      {
        id:1,
        name:'豆角',
        count:0
      },
     {
       id:2,
       name:'花菜',
       count:0
     },
     {
       id:3,
       name:'白菜',
       count:0
     },
    ],
    soup:
    [
      {
        id:1,
        name:'绿豆汤',
        count:0
      },
     {
       id:2,
       name:'大米汤',
       count:0
     },
     {
       id:3,
       name:'小米汤',
       count:0
     },
    ],
    others:
    [
      {
        content:'',
        picture:''
      }
    ]
  },

  submit(){
     var assistedFood = []
     var meatDishs = []
     var soup = []
     var stapleFood = []
     var vegetableDishs = []
     for(var i in this.data.assistedFood){
       if(this.data.assistedFood[i].count!=0){
         assistedFood.push(this.data.assistedFood[i])
       }
     }
     for(var i in this.data.meatDishs){
      if(this.data.meatDishs[i].count!=0){
        meatDishs.push(this.data.meatDishs[i])
      }
    }
    for(var i in this.data.soup){
      if(this.data.soup[i].count!=0){
        soup.push(this.data.soup[i])
      }
    }
    for(var i in this.data.stapleFood){
      if(this.data.stapleFood[i].count!=0){
        stapleFood.push(this.data.stapleFood[i])
      }
    }
    for(var i in this.data.vegetableDishs){
      if(this.data.vegetableDishs[i].count!=0){
        vegetableDishs.push(this.data.vegetableDishs[i])
      }
    }
     wx.request({
       url: 'https://zhixiao.site:8080/client/dinner/add',
       method:'POST',
       header: {  
        "Content-Type": "application/json"
      },
       data:
       {
         elderId:this.data.elderId,
         stapleFood:stapleFood,
         assistedFood:assistedFood,
         meatDishs:meatDishs,
         vegetableDishs:vegetableDishs,
         soup:soup,
         others:this.data.others
       },
       success:(res)=>{
        if(res.data.msg==='重复添加'){
          Toast('今日已上传！');
          return
         }
          console.log("submit success")
          console.log(res)
          Toast('上传成功');
       }
     })
   },

  onChange(event) {
   var index = event.currentTarget.dataset.index
   const soup = this.data.soup
   soup[index].count = event.detail
   this.setData({
    soup:soup
   })
  },
  onChange1(event) {
    var index = event.currentTarget.dataset.index
    const stapleFood = this.data.stapleFood
    stapleFood[index].count = event.detail
    this.setData({
      stapleFood:stapleFood
    })
   },
   onChange2(event) {
    var index = event.currentTarget.dataset.index
    const assistedFood = this.data.assistedFood
    assistedFood[index].count = event.detail
    this.setData({
      assistedFood:assistedFood
    })
   },

   onChange3(event) {
    var index = event.currentTarget.dataset.index
    const meatDishs = this.data.meatDishs
    meatDishs[index].count = event.detail
    this.setData({
      meatDishs:meatDishs
    })
   },

   onChange4(event) {
    var index = event.currentTarget.dataset.index
    const vegetableDishs = this.data.vegetableDishs
    vegetableDishs[index].count = event.detail
    this.setData({
      vegetableDishs:vegetableDishs
    })
   },
   onChange5(e){
    var others = this.data.others
    others[0].content = e.detail.value
    this.setData({
      others:others
    })
  },
  photo(){
    var that = this
    wx.chooseMedia({
      count:1,
      mediaType:['image'],
      sourceType:['album','camera'],
      camera:'back',
      success(res){
        console.log(that)
        var others = that.data.others
        wx.uploadFile({
          url: 'https://zhixiao.site:8080/activity/upload', // 仅为示例，非真实的接口地址
          filePath: res.tempFiles[0].tempFilePath,
          name: 'file',
          formData: { user: 'test' },
          success(res1) {
            // 上传完成需要更新 fileList
            console.log(res1)
            console.log(res1.data)
            var url = res1.data.slice(32,res1.data.length-3)
            console.log(url)
            //注意client
            // others[0].picture =  url
            others[0].picture =  url.replace('/client','')
            that.setData({
              others:others
            })
          },
        });
        
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      elderId:options.id
    })
    //this.query()
  },
  query(){
    
    wx.request({
      url: 'https://zhixiao.site:8080/client/lunch/query',
      data:{
        elderId:this.data.elderId
      },
      success:(res)=>{
        if(res.data.data.length===0){
          return
        }
        this.setData({
          soup:res.data.data[0].soup,
          stapleFood:res.data.data[0].stapleFood,
          assistedFood:res.data.data[0].assistedFood,
          meatDishs:res.data.data[0].meatDishs,
          vegetableDishs:res.data.data[0].vegetableDishs,
          others:res.data.data[0].others
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})